<script setup lang="ts">
import VDialog from "@/components/VDialog/VDialog.vue";
import {computed, ref, watch} from "vue";
import SirSelfCheckIndex from "./selfCheckIndex.vue";
import { saveSirSelfCheckApi } from "@/api/airport/dataManage/sirSelfCheck";
import { ElMessage } from "element-plus";
import { addInspectionCheckApi } from "@/api/airport/inspection/inspectionCheck";
import { useRoute } from "vue-router";

interface Props {
  type: "add" | "update";
  modelValue: boolean;
  row?: any;
}

const props = defineProps<Props>();
const emits = defineEmits<{
  (e: "update:modelValue", v: boolean): void;
  (e: "success"): void;
}>();

const route = useRoute();
const inspectionId = route.query.inspectionId as string;
const visible = computed({
  get: () => props.modelValue,
  set(v) {
    emits("update:modelValue", v);
  }
});

const loading = ref(false);
const selectedData = ref<any[]>([]);

// 处理选中项变化
const handleSelectionChange = (selection: any[]) => {
  selectedData.value = selection;
};

// 修改 handleConfirm 方法，不再调用 addInspectionCheckApi
async function handleConfirm() {
  try {
    ElMessage.success("操作成功");
    visible.value = false;
    emits("success"); // 触发成功事件
  } catch (e) {
    console.error(e);
    ElMessage.error((e as Error)?.message || "操作失败");
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <v-dialog
    show-full-screen
    :fixed-body-height="false"
    use-body-scrolling
    :title="type === 'add' ? '新增检查项' : '更新检查项'"
    v-model="visible"
    :loading="loading"
    @confirm="handleConfirm"
    @cancel="visible = false"
    width="80%"
    style="max-height: 140vh; overflow-y: auto;"
  >
    <!-- 传递 inspectionId 给 selfCheckIndex 组件 -->
    <sir-self-check-index
      :inspection-id="inspectionId"
      @success="emits('success')"
    />
  </v-dialog>
</template>
